package ru.reaperoq.inventory.screens.main

import LoadingStatus
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.QrCodeScanner
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SecondaryTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import my.MyObjectsViewModel
import my.models.MyObjectsEvent
import org.koin.androidx.compose.navigation.koinNavViewModel
import ru.reaperoq.inventory.components.lists.ComponentsList
import ru.reaperoq.inventory.components.lists.ComputersList

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyLocationScreen(
    modifier: Modifier = Modifier,
    viewModel: MyObjectsViewModel = koinNavViewModel(),
    onOpenScanner: () -> Unit
) {
    var selectedTabIndex by remember { mutableIntStateOf(0) }
    val viewState by viewModel.viewStates().collectAsState()
    LaunchedEffect(Unit) {
        viewModel.obtainEvent(MyObjectsEvent.LoadComputers)
        viewModel.obtainEvent(MyObjectsEvent.LoadComponents)
    }

    Scaffold(
        modifier = modifier,
        topBar = {
            SecondaryTabRow(selectedTabIndex = selectedTabIndex) {
                Tab(
                    text = {
                        Text(text = "Компоненты")
                    },
                    onClick = { selectedTabIndex = 0 },
                    selected = selectedTabIndex == 0
                )
                Tab(
                    text = {
                        Text(text = "Компьютеры")
                    },
                    onClick = { selectedTabIndex = 1 },
                    selected = selectedTabIndex == 1
                )
            }
        },
        floatingActionButton = {
            FloatingActionButton(onClick = onOpenScanner) {
                Icon(
                    imageVector = Icons.Default.QrCodeScanner,
                    contentDescription = null
                )
            }
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .consumeWindowInsets(padding)
                .padding(padding)
        ) {
            if (viewState.loadingStatus is LoadingStatus.Loading) {
                LinearProgressIndicator(Modifier.fillMaxWidth())
            }
            when (selectedTabIndex) {
                0 -> {
                    ComponentsList(
                        modifier = Modifier
                            .fillMaxSize(),
                        components = viewState.components
                    )
                }

                1 -> {
                    ComputersList(
                        modifier = Modifier
                            .fillMaxSize(),
                        computers = viewState.computers
                    )
                }
            }
        }
    }
}